/*
 * Created on 16.01.2004
 * Local Projects Database
 * Mihai Postelnicu (mihai@ro-gateway.org)
 * (c) 2003 by eRomania Gateway
 */
package ro.gateway.aida.db;

import ro.gateway.aida.obj.BreadCrumb;
import ro.gateway.aida.obj.Country;
import ro.gateway.aida.obj.Language;
import ro.gateway.aida.obj.db.BreadCrumbDB;
import ro.gateway.aida.obj.db.CountryDB;
import ro.gateway.aida.obj.db.LanguageDB;
import ro.gateway.aida.usr.messaging.Alerter;
import ro.gateway.aida.usr.messaging.AlerterDB;
import ro.gateway.aida.usr.messaging.Message;
import ro.gateway.aida.usr.messaging.MessageDB;
import ro.gateway.aida.usr.messaging.ProjectFilter;
import ro.gateway.aida.usr.messaging.ProjectFilterDB;
import ro.xblue.translator.TrnHarvestLocation;
import ro.xblue.translator.TrnHarvestLocationDB;


/**
 * @author Mihai Postelnicu<p>
 *
 *  e-mail (mihai@ro-gateway.org)<br>
 * (c) 2003 by eRomania Gateway<p>
 *  This class transparently handles Persistence Objects<br>
 *  DBWrapper detaches business objects from application by being the only business object that
 *  is directly invoked
 */
public class DBWrapper extends DBPersistenceManager implements DBWrapable {
    private DBWrapper(PersistenceToken token) {
        super(token);
    }

    public static DBWrapper getManager(PersistenceToken token) {
        return new DBWrapper(token);
    }

    public Object trackManager(Object o) throws Exception {
        if (!o.getClass().getInterfaces()[0].getName().equals("ro.gateway.aida.db.PersistentObject"))
            throw new UndeclaredPersistentObjectException();

        if (o instanceof Message)
            return MessageDB.getManager(token);

        if (o instanceof ProjectFilter)
            return ProjectFilterDB.getManager(token);

        if (o instanceof Alerter)
            return AlerterDB.getManager(token);

        if (o instanceof Country)
            return CountryDB.getManager(token);

        if (o instanceof BreadCrumb)
            return BreadCrumbDB.getManager(token);

        if (o instanceof TrnHarvestLocation)
            return TrnHarvestLocationDB.getManager(token);

        
		if (o instanceof Language)
				 return LanguageDB.getManager(token);

        //TODO:add managers here
        throw new UnidentifiedPersistentObjectException();
    }

    public void synchronize(Object o) throws Exception {
        DBWrapable so = (DBWrapable) (trackManager(o));
        so.synchronize(o);
    }

    public void delete(Object o) throws Exception {
        DBWrapable so = (DBWrapable) (trackManager(o));
        so.delete(o);
    }

    public Object get(Object o) throws Exception {
        DBWrapable so = (DBWrapable) (trackManager(o));

        return so.get(o);
    }
}
